package com.qianxx.viewlibrary.text.randomnumber;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.qianxx.viewlibrary.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 带随机动画的数值控件
 */
public class RandomNumber extends View {
    Paint paint = new Paint();

    //动画持续时间
    private long duration = 700;
    //百分比
    private float percent;
    //小数点所在位置
    private int index;
    //字体大小
    private float textSize = 40;
    //数目
    private double number;
    //字体颜色
    private int textColor = Color.BLACK;

    public RandomNumber(Context context) {
        this(context, null);
    }

    public RandomNumber(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RandomNumber(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.AnimView);
        textSize = (int) ta.getDimension(R.styleable.AnimView_TextSize, 40);
        textColor = (int) ta.getColor(R.styleable.AnimView_TextColor, Color.BLACK);
        ta.recycle();

        paint.setTextSize(textSize);
        paint.setColor(textColor);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width + getTextWidth() / 2, height + getTextHeight() / 6);
    }

    /**
     * 根据xml的设定获取宽度
     *
     * @param measureSpec
     * @return
     */
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.AT_MOST) { //wrap_content
            specSize = width();
        } else if (specMode == MeasureSpec.EXACTLY) {//fill_parent或者精确值
            specSize = width();
        }
        return specSize;

    }

    /**
     * 根据xml的设定获取高度
     *
     * @param measureSpec
     * @return
     */
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.AT_MOST) {//wrap_content
            specSize = getTextHeight();
        } else if (specMode == MeasureSpec.EXACTLY) {//fill_parent或者精确值
            specSize = getTextHeight();
        }
        return specSize;
    }

    public float getPercent() {
        return percent;
    }

    public void setPercent(float percent) {
        this.percent = percent;
        invalidate();
    }

    public void setNumber(double number) {
        this.number = number;
        startAnimation();
    }

    public double getNumber() {
        return number;
    }

    /**
     * 保留2位小数
     *
     * @param number
     * @return
     */
    public String format(double number) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return decimalFormat.format(number);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawText(format(number * percent) + "",
                0, getTextHeight(), paint);
    }

    /**
     * 获取字体颜色
     *
     * @return
     */
    public int getTextColor() {
        return textColor;
    }

    /**
     * 设置字体颜色
     *
     * @param textColor
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
        paint.setColor(textColor);
        invalidate();
    }

    /**
     * 获取随机数
     *
     * @return
     */
    private int getRandom() {
        return new Random().nextInt((int) number);
    }

    /**
     * 获取单个数字的宽度
     *
     * @return
     */
    private int getTextWidth() {
        Rect rect = new Rect();
        paint.setTextSize(textSize);
        paint.getTextBounds("9", 0, "9".length(), rect);
        return rect.width();
    }

    /**
     * 获取单个数字的高度
     *
     * @return
     */
    private int getTextHeight() {
        Rect rect = new Rect();
        paint.setTextSize(textSize);
        paint.getTextBounds("9", 0, "9".length(), rect);
        return rect.height();
    }

    /**
     * 整个数值的宽度
     *
     * @return
     */
    private int width() {
        Rect rect = new Rect();
        paint.setTextSize(textSize);
        paint.getTextBounds(number + "", 0, (number + "").length(), rect);
        return rect.width();
    }

    /**
     * 开始执行动画
     */
    private void startAnimation() {
        ObjectAnimator an = ObjectAnimator.ofFloat(this, "percent", 0, 1);
        an.setInterpolator(new DecelerateInterpolator());
        an.setDuration(duration);
        an.start();
    }

}
